import django_filters
from assets.models import *
from fort.models import *
from dbmanager.models import *
import datetime
import json


is_expired_types = (
        (0, '未过期'),
        (1, '已过期'),
    )

class AssetsFilter(django_filters.rest_framework.FilterSet):
    asset_nu = django_filters.CharFilter(lookup_expr='icontains')
    asset_management_ip = django_filters.CharFilter(lookup_expr='contains', field_name='asset_management_ip__IP')
    is_expired = django_filters.ChoiceFilter(label='is_expired', choices=is_expired_types, method='filter_is_expired')
    asset_purchase_day_start = django_filters.DateTimeFilter(label='asset_purchase_day_start', method='filter_asset_purchase_day_start')
    asset_purchase_day_end = django_filters.DateTimeFilter(label='asset_purchase_day_end', method='filter_asset_purchase_day_end')
    asset_expire_day_start = django_filters.DateTimeFilter(label='asset_expire_day_start', method='filter_asset_expire_day_start')
    asset_expire_day_end = django_filters.DateTimeFilter(label='asset_expire_day_end', method='filter_asset_expire_day_end')
    ids = django_filters.CharFilter(label='ids', method='filter_ids')

    class Meta:
        model = Assets
        fields = ['asset_environment', 'asset_type', 'asset_nu', 'asset_management_ip', 'asset_status', 'is_expired', 'asset_purchase_day_start', 'asset_purchase_day_end', 'asset_expire_day_start', 'asset_expire_day_end', 'asset_product']

    def filter_is_expired(self, queryset, name, value):
        if int(value) == 1:
            return queryset.filter(asset_expire_day__lt=datetime.date.today())
        else:
            return queryset.filter(asset_expire_day__gte=datetime.date.today())

    def filter_asset_purchase_day_start(self, queryset, name, value):
        return queryset.filter(asset_purchase_day__gte=value)

    def filter_asset_purchase_day_end(self, queryset, name, value):
        return queryset.filter(asset_purchase_day__lte=value)

    def filter_asset_expire_day_start(self, queryset, name, value):
        return queryset.filter(asset_expire_day__gte=value)

    def filter_asset_expire_day_end(self, queryset, name, value):
        return queryset.filter(asset_expire_day__lte=value)

    def filter_ids(self, queryset, name, value):
        ids = json.loads(value)
        return queryset.filter(id__in=ids)


class FortListFilter(django_filters.rest_framework.FilterSet):
    asset_nu = django_filters.CharFilter(label='asset_nu',  method='filter_asset_nu')
    server_ip = django_filters.CharFilter(label='server_ip', method='filter_server_ip')
    asset_username = django_filters.CharFilter(label='asset_username', method='filter_asset_username')
    asset_environment = django_filters.CharFilter(label='asset_environment', method='filter_asset_environment')
    asset_product = django_filters.CharFilter(label='asset_product', field_name='server__asset_management_ip__asset_product')

    class Meta:
        model = FortServer
        fields = ['asset_nu', 'server_ip', 'server_protocol', 'server_status', 'asset_username', 'asset_product']

    def filter_asset_nu(self, queryset, name, value):
        return queryset.filter(server__asset_management_ip__asset_nu__icontains=value).distinct()

    def filter_server_ip(self, queryset, name, value):
        if value == '.':
            return queryset.all()
        else:
            return queryset.filter(server__IP__icontains=value)

    def filter_asset_username(self, queryset, name, value):
        return queryset.filter(fortserveruser__fort_username__icontains=value).distinct()

    def filter_asset_environment(self, queryset, name, value):
        return queryset.filter(server__asset_management_ip__asset_environment=value).distinct()


class FortUserListFilter(django_filters.rest_framework.FilterSet):
    server_protocol = django_filters.CharFilter(label='server_protocol', field_name='fort_server__server_protocol')
    server_ip = django_filters.CharFilter(label='server_ip', method='filter_server_ip')
    asset_nu = django_filters.CharFilter(label='asset_nu', method='filter_asset_nu')
    asset_product = django_filters.CharFilter(label='asset_product', field_name='fort_server__server__asset_management_ip__asset_product')
    asset_environment = django_filters.CharFilter(label='asset_environment', method='filter_asset_environment')

    class Meta:
        model = FortServerUser
        fields = ['server_protocol', 'server_ip', 'asset_nu', 'fort_username', 'asset_product']

    def filter_server_ip(self, queryset, name, value):
        if value == '.':
            return queryset.all()
        else:
            return queryset.filter(fort_server__server__IP__icontains=value)

    def filter_asset_nu(self, queryset, name, value):
        return queryset.filter(fort_server__server__asset_management_ip__asset_nu__icontains=value).distinct()

    def filter_asset_environment(self, queryset, name, value):
        return queryset.filter(fort_server__server__asset_management_ip__asset_environment=value).distinct()


class DBConfigListFilter(django_filters.rest_framework.FilterSet):
    asset_environment = django_filters.CharFilter(label='asset_environment', field_name='db_server__asset_environment')
    db_ip = django_filters.CharFilter(label='db_ip', method='filter_db_ip')
    asset_nu = django_filters.CharFilter(label='asset_nu', method='filter_asset_nu')
    asset_product = django_filters.CharFilter(label='asset_product', field_name='db_server__asset_product')
    db_user = django_filters.CharFilter(lookup_expr='icontains', field_name='db_user')
    asset_environment = django_filters.CharFilter(label='asset_environment', field_name='db_server__asset_environment')
    ids = django_filters.CharFilter(label='ids', method='filter_ids')

    class Meta:
        model = DBConfig
        fields = ['db_type', 'db_user', 'db_ip', 'asset_nu', 'asset_environment', 'asset_product']

    def filter_db_ip(self, queryset, name, value):
        if value == '.':
            return queryset.all()
        else:
            return queryset.filter(db_server__asset_management_ip__IP__icontains=value)

    def filter_asset_nu(self, queryset, name, value):
        return queryset.filter(db_server__asset_nu__icontains=value).distinct()

    def filter_ids(self, queryset, name, value):
        ids = json.loads(value)
        return queryset.filter(id__in=ids)
